package com.easybenefit.UUService.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.SparseArray;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.TextView;

import com.easybeanfit.UUService.commutill.UUJsonUtil;
import com.easybeanfit.UUService.commutill.UUNetUtill;
import com.easybeanfit.UUService.commutill.UUSharedUtil;
import com.easybeanfit.UUService.commutill.UUToastUtil;
import com.easybeanfit.UUService.config.UUConfig;
import com.easybeanfit.UUService.config.UUDict;
import com.easybenefit.UUService.BaseActivity;
import com.easybenefit.UUService.R;
import com.easybenefit.UUService.adapter.CouponsListAdapter;
import com.easybenefit.UUService.model.CouponlistBean;
import com.easybenefit.UUService.model.HttpCouponBean;
import com.easybenefit.UUService.model.Industry;
import com.easybenefit.UUService.model.UserInfo;
import com.easybenefit.UUService.myinterface.IActivityInterface;
import com.easybenefit.UUService.screenmenu.DoubleListFilterView;
import com.easybenefit.UUService.screenmenu.SingleListFilterView;
import com.easybenefit.UUService.xlist.XListView;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.SyncHttpClient;

import org.apache.http.Header;

import java.util.ArrayList;
import java.util.List;

/**
 * 优惠券列表
 */
public class CouponsListActivity extends BaseActivity implements IActivityInterface, View.OnClickListener {
    private TextView tv_title;//标题
    private XListView xListView;
    private CouponsListAdapter couponsListAdapter;
    private Button btn_add;
    private Button add_coupon_button;
    private Button btn_back;
    private List<CouponlistBean> list;
    private int page = 1;
    private int rows = 10;
    private Context context;
    private UserInfo userInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_coupons_list);
        initView();
        initData();
        initListener();
    }

    @Override
    public void initData() {
        context = this;
        list = new ArrayList<CouponlistBean>();
        couponsListAdapter = new CouponsListAdapter(list, context, 0);
        xListView.setAdapter(couponsListAdapter);
        btn_add.setText(getResources().getText(R.string.coupon_caculate));
        userInfo = (UserInfo) UUSharedUtil.getObject(context, UUConfig.UU_USERINFO_NAME, UserInfo.class);
        tv_title.setText(userInfo.getMer_name());
    }

    @Override
    protected void onResume() {
            super.onResume();
            getCoupData();
            couponsListAdapter.notifyDataSetChanged();

    }

    @Override
    public void initView() {
        xListView = (XListView) findViewById(R.id.xlist_coupons);
        tv_title = (TextView) findViewById(R.id.top_title);
        btn_add = (Button) findViewById(R.id.btn_add);
        add_coupon_button = (Button) findViewById(R.id.add_coupon_button);
        btn_back = (Button) findViewById(R.id.btn_back);
        xListView.setPullLoadEnable(false);
        xListView.setPullRefreshEnable(true);

    }

    /**
     * 加载结束需要执行的方法
     */
    public void onload() {
        xListView.stopRefresh();
        xListView.stopLoadMore();
        xListView.setRefreshTime();
    }

    @Override
    public void initListener() {
        btn_back.setOnClickListener(this);
        btn_add.setOnClickListener(this);
        xListView.setXListViewListener(new XListView.IXListViewListener() {
            @Override
            public void onRefresh() {
                page = 1;

                getCoupData();

            }

            @Override
            public void onLoadMore() {
                page++;

                getCoupData();
            }

        });
        xListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                TextView tv_url = (TextView) view.findViewById(R.id.tv_item_coupons_url);
                CouponlistBean coup = list.get(position - 1);
                Intent intent = new Intent(CouponsListActivity.this, CouponsDetailActivity.class);
                intent.putExtra("type", false);
                intent.putExtra("umeng_title", coup.getSharename());//TODO
                intent.putExtra("umeng_img", coup.getShareimg());
                intent.putExtra("umeng_url", coup.getShareurl());
                intent.putExtra("url", tv_url.getText().toString());
                intent.putExtra("coupon_id", coup.getCoupon_id());
                intent.putExtra("is_col",coup.getIs_col());//是否为U卡券
                startActivity(intent);
            }
        });
        add_coupon_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkadd();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_back:
                CouponsListActivity.this.finish();
                break;
            case R.id.btn_add:
                //跳转统计核算
                Intent intent = new Intent(CouponsListActivity.this, CouponsDetailActivity.class);
                intent.putExtra("url", UUConfig.CACULATE_URL + userInfo.getMer_id());
                intent.putExtra("type", true);
                intent.putExtra("noWX", true);
                startActivity(intent);
                break;
        }
    }

    /**
     * 查找数据
     */
    public void getCoupData() {

        if (!UUNetUtill.isConnected(CouponsListActivity.this)) {
            UUToastUtil.showToast(CouponsListActivity.this, UUDict.NOT_INTERNET);
        } else {
            /** 获取优惠券列表 */
            String url = UUConfig.SERVER_BASE_URL + "newcoulist";
            RequestParams params = new RequestParams();
            UserInfo userInfo = (UserInfo) UUSharedUtil.getObject(CouponsListActivity.this, UUConfig.UU_USERINFO_NAME, UserInfo.class);
            params.put("mer_id", userInfo.getMer_id());
            AsyncHttpClient client = new AsyncHttpClient();
            client.post(url, params, new AsyncHttpResponseHandler() {
                @Override
                public void onStart() {

                }

                @Override
                public void onFinish() {
                    onload();
                }

                @Override
                public void onSuccess(int i, Header[] headers, byte[] bytes) {
                    String content = new String(bytes);
                    //清除数据以后需要马上更新一下adapter
                    list.clear();
                    HttpCouponBean httpCouponBean = (HttpCouponBean) UUJsonUtil.fromJson(content, HttpCouponBean.class);
                    list.addAll(httpCouponBean.getData().getCouponlist());
                    couponsListAdapter.notifyDataSetChanged();
                }

                @Override
                public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {

                }
            });

        }

    }

    /**
     * 判断是否有权限上架优惠券
     */
    public void checkadd() {

        if (!UUNetUtill.isConnected(CouponsListActivity.this)) {
            UUToastUtil.showToast(CouponsListActivity.this, UUDict.NOT_INTERNET);
        } else {
            /** 是否有权限上架优惠券 */
            String url = UUConfig.SERVER_BASE_URL + "checkadd";
            RequestParams params = new RequestParams();
            UserInfo userInfo = (UserInfo) UUSharedUtil.getObject(CouponsListActivity.this, UUConfig.UU_USERINFO_NAME, UserInfo.class);
            params.put("mer_id", userInfo.getMer_id());
            AsyncHttpClient client = new AsyncHttpClient();
            client.post(url, params, new AsyncHttpResponseHandler() {
                @Override
                public void onStart() {

                }

                @Override
                public void onFinish() {
                    onload();
                }

                @Override
                public void onSuccess(int i, Header[] headers, byte[] bytes) {
                    String content = new String(bytes);
                    HttpCouponBean httpCouponBean = (HttpCouponBean) UUJsonUtil.fromJson(content, HttpCouponBean.class);
                    if (httpCouponBean.getCode().equals("200")) {
                        Intent intent = new Intent(CouponsListActivity.this, CouponActivity.class);
                        startActivity(intent);
                    } else {
                        new AlertDialog.Builder(CouponsListActivity.this).setTitle("系统提示")
                                .setMessage(httpCouponBean.getMessage()).
                                setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                    }
                                }).show();
                    }
                }

                @Override
                public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {

                }
            });

        }
    }
}
